// pages/member-points/member-points.js
import api from '../../utils/api.js'
let app = getApp();
import {
    HTTP
} from '../../utils/http.js';
import { config } from '../../utils/config'
let http = new HTTP();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        showModel: false, // 模态窗口
        showDetail: false, //展示 规则
        new_integral_num: 0, //总积分
        integrallist: [], //积分变动
        imgUrl: config.imgUrl,
    },

    // 展示规则
    showDetail(e) {
        let showDetail = this.data.showDetail;
        let showModel = this.data.showModel;

        this.setData({
            showDetail: !showDetail,
            showModel: !showModel
        })

    },
    close(e) {
        this.setData({
            showDetail: false,
            showModel: false
        })
    },
    onLoad: function(options) {
        let user_id = wx.getStorageSync('user_id');
        this.getIntegralList(user_id);
        this.getIntegralRule(1).then((res) => {

            let employ = res.employ.split('；');
            let gain = res.gain.split('；')
            this.setData({
                rule: res,
                employ,
                gain
            })
        })

    },
    //拿到用户的积分数以及积分使用历史
    getIntegralList(id) {
        http.request({
            url: api.getIntegralList,
            data: {
                id: id
            },
            success: (res) => {
                console.log(res)
                if (res.error_code == 40003) {
                    this.setData({
                        errMsg: "您还没有积分历史"
                    })
                }
                if (res.code == 200) {
                    let integrallist = res.data.integrallist;
                    let new_integral_num = res.data.new_integral_num
                    this.setData({
                        new_integral_num,
                        integrallist
                    })
                }
            }
        })
    },
    //拿到规则——1代表积分规则——2代表银歌币规则
    getIntegralRule(id) {
        return new Promise((resolve) => {
            http.request({
                url: api.getIntegralRule,
                data: {
                    id: id
                },
                success: (res) => {
                    console.log('拿到的规则', res)
                    resolve(res)
                }
            })
        })

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

})